﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ershou.BLL;

public partial class gly_spsh : System.Web.UI.Page
{
    sp sp1 = new sp();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Key"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        if(Session["Key"].ToString() != "3")
        {
            Response.Redirect("~/Default.aspx");
        }
        if (!IsPostBack)
        {
            Bind();  //调用自定义方法Bind()
        }
    }

    protected void Bind()
    {
        var orders = sp1.GetAllOrder();
        if (orders.Count() == 0)
        {
            Panel1.Visible = false;
            lblOrder.Text = "尚无订单！";
        }
        else
        {
            Panel1.Visible = true;
            lblOrder.Text = "";
        }
        GridView1.DataSource = orders;
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Bind();  //调用自定义方法Bind()
    }

    protected void btnAudit_Click(object sender, EventArgs e) 
    {
        try
        {
            GridView gvOrder = new GridView();
            gvOrder = (GridView)Page.Master.FindControl("ContentPlaceHolder1").FindControl("GridView1");
            if (gvOrder != null)
            {
                for (int i = 0; i < gvOrder.Rows.Count; i++)
                {
                    CheckBox chkChoice = new CheckBox();
                    chkChoice = (CheckBox)gvOrder.Rows[i].FindControl("CheckBox1");
                    if (chkChoice != null)
                    {
                        if (chkChoice.Checked)
                        {
                            int OrderId = int.Parse(gvOrder.Rows[i].Cells[2].Text);
                            sp1.UpdateOrder(OrderId);  //调用自定义方法UpdateOrder(）
                        }
                    }
                }
            }
            Label1.Text = "审核成功！";
        }
        catch
        {
            Label1.Text = "审核失败！";
        }
        Bind();  //调用自定义方法Bind()
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            GridView gvOrder = new GridView();
            gvOrder = (GridView)Page.Master.FindControl("ContentPlaceHolder1").FindControl("GridView1");
            if (gvOrder != null)
            {
                for (int i = 0; i < gvOrder.Rows.Count; i++)
                {
                    CheckBox chkChoice = new CheckBox();
                    chkChoice = (CheckBox)gvOrder.Rows[i].FindControl("CheckBox1");
                    if (chkChoice != null)
                    {
                        if (chkChoice.Checked)
                        {
                            int OrderId = int.Parse(gvOrder.Rows[i].Cells[2].Text);
                            sp1.UpdateOrder2(OrderId);  //调用自定义方法UpdateOrder(）
                        }
                    }
                }
            }
            Label1.Text = "下架成功！";
        }
        catch
        {
            Label1.Text = "下架失败！";
        }
        Bind();  //调用自定义方法Bind()
    }
}